home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Table / Sources / View.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  15.3 KB  |  517 lines  |  [TEXT/CWIE]

  1. //========================================================================================
  2. //
  3. //    File:                View.cpp
  4. //    Release Version:    $ ODF 2 $ 
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef VIEW_H
  11. #include "View.h"
  12. #endif
  13.  
  14. #ifndef FRAME_H
  15. #include "Frame.h"
  16. #endif
  17.  
  18. #ifndef CONTENT_H
  19. #include "Content.h"
  20. #endif
  21.  
  22. #ifndef TRACKER_H
  23. #include "Tracker.h"
  24. #endif
  25.  
  26. #ifndef LINKING_H
  27. #include "Linking.h"
  28. #endif
  29.  
  30. #ifndef SELECTION_H
  31. #include "Selection.h"
  32. #endif
  33.  
  34. #ifndef FWCONTXT_H
  35. #include "FWContxt.h"
  36. #endif
  37.  
  38. #ifndef FWITERS_H
  39. #include "FWIters.h"
  40. #endif
  41.  
  42. #ifndef FWLNKITE_H
  43. #include "FWLnkIte.h"
  44. #endif
  45.  
  46. #ifndef FWFCTCLP_H
  47. #include "FWFctClp.h"
  48. #endif
  49.  
  50. #ifndef FWODMISC_H
  51. #include "FWODMisc.h"
  52. #endif
  53.  
  54. #ifndef FWRGNSHP_H
  55. #include "FWRgnShp.h"
  56. #endif
  57.  
  58. #ifndef FWTOOLBX_H
  59. #include "FWToolBx.h"
  60. #endif
  61.  
  62. #ifndef FWMNUBAR_H
  63. #include "FWMnubar.h"
  64. #endif
  65.  
  66. //========================================================================================
  67. // RunTime Info
  68. //========================================================================================
  69.  
  70. FW_DEFINE_CLASS_M1(CTableView, FW_CSuperView)
  71.  
  72. const FW_ClassTypeConstant LTableView = FW_TYPE_CONSTANT('t','b','v','w');
  73. FW_REGISTER_ARCHIVABLE_CLASS(LTableView, CTableView, CTableView::Create, FW_CView::Read, CTableView::Destroy, FW_CView::Write)
  74.  
  75. //========================================================================================
  76. //    class CTableView
  77. //========================================================================================
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // CTableView::CTableView
  81. //----------------------------------------------------------------------------------------
  82.  
  83. CTableView::CTableView(Environment* ev) :
  84.     FW_CSuperView(ev),
  85.     fTablePart(NULL),
  86.     fTableFrame(NULL),
  87.     fTableContent(NULL),
  88.     fGridShown(TRUE),
  89.     fHighlight(kODNoHighlight)
  90. {    
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // CTableView::~CTableView
  95. //----------------------------------------------------------------------------------------
  96.  
  97. CTableView::~CTableView()
  98. {
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. // CTableView::Draw
  103. //----------------------------------------------------------------------------------------
  104.  
  105. void CTableView::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  106. {
  107.     FW_CViewContext fc(ev, this, odFacet, invalidShape);
  108.     
  109.     FW_CRect invalidRect;
  110.     fc.GetClipRect(invalidRect);
  111.     
  112.     // Erase invalid portion of the frame
  113.     FW_CRectShape::RenderRect(fc, invalidRect, FW_kFill, FW_kWhiteEraseInk);
  114.     
  115.     DrawGray(ev, fc);
  116.     
  117.     // Draw the table grid
  118.     if (fGridShown)
  119.         DrawGrid(ev, fc, TRUE);    
  120.  
  121.     // Draw the selection
  122.     if (fTableFrame->HasSelectionFocus(ev) && fGridShown && (fHighlight == kODFullHighlight))
  123.         DrawCellHighlight(ev, fc, fTableFrame->GetSelection(ev)->GetSelectedCell());
  124.  
  125.     //--- Draw Link borders ---
  126.     FW_CAcquiredODWindow aqODWindow = odFacet->GetFrame(ev)->AcquireWindow(ev);
  127.     if (aqODWindow->ShouldShowLinks(ev))
  128.     {
  129.         //--- Draw borders around links ---
  130.         FW_CLinkIterator it(fTablePart->GetLinkManager(ev));
  131.         for (FW_CLink* link = it.FirstLink(); it.IsNotComplete(); link = it.NextLink())
  132.         {
  133.             link->ShowHideLinkBorder(ev, true, odFacet);
  134.         }
  135.     }
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. //    CTableView::DrawGray
  140. //----------------------------------------------------------------------------------------
  141.  
  142. void CTableView::DrawGray(Environment* ev, FW_CGraphicContext& gc)
  143. {    
  144.     FW_CRect contentRect = GetBoundsInContent(ev);
  145.     FW_CRect extentRect(FW_kZeroPoint, fTableContent->GetExtent());
  146.             
  147.     FW_CAcquiredODShape shape1 = ::FW_NewODShape(ev, contentRect);
  148.     FW_CAcquiredODShape shape2 = ::FW_NewODShape(ev, extentRect);
  149.     shape1->Subtract(ev, shape2);
  150.     if (!shape1->IsEmpty(ev))
  151.     {
  152.         FW_CRegionShape::RenderRegion(gc, shape1, FW_kFill, FW_kRGBLightGray);
  153.         
  154.         extentRect.Inset(FW_kFixedNeg1, FW_kFixedNeg1);
  155.         FW_CRectShape::RenderRect(gc, extentRect, FW_kFrame);
  156.     }
  157. }
  158.  
  159. //----------------------------------------------------------------------------------------
  160. //    CTableView::DrawCellHighlight
  161. //----------------------------------------------------------------------------------------
  162.  
  163. void CTableView::DrawCellHighlight(Environment* ev, FW_CViewContext& fc, const CCell& cell)
  164. {
  165.     CTableProxy* proxy = fTableContent->CellToProxy(cell);
  166.     if (proxy == NULL)
  167.     {
  168.         FW_CRect rect;
  169.         fTableContent->FindRect(cell, rect);
  170.         FW_CRectShape::RenderRect(fc, rect, FW_kFill, FW_CInk(FW_kSystemHilite));
  171.     }
  172.     else
  173.     {
  174.         FW_CAcquiredODShape hiliteShape = proxy->AcquireHiliteShape(ev, cell, fTableFrame);
  175.         FW_CRegionShape::RenderRegion(fc, hiliteShape, FW_kFill, FW_CInk(FW_kSystemHilite));
  176.     }
  177. }
  178.  
  179. //---------------------------------------------------------------------------------------
  180. //    CTableView::DrawGrid
  181. //---------------------------------------------------------------------------------------
  182.  
  183. void CTableView::DrawGrid(Environment* ev, FW_CGraphicContext& gc, FW_Boolean gray)
  184. {
  185. FW_UNUSED(ev);
  186.     // Get the frame's rect at (0,0)
  187.     FW_CRect rect(FW_kZeroPoint, fTableContent->GetExtent());
  188.     
  189.     // Draw the table grid
  190.     FW_CLineShape lineShape;
  191.     lineShape.GetStyle().SetPattern(gray ? FW_kGrayPat : FW_kWhitePat);
  192.         
  193.     FW_Fixed xy;
  194.     short rc;
  195.     for(xy = rect.top, rc = 0; xy < rect.bottom; xy += fTableContent->GetHeight(rc++) + kBorderHeight)
  196.     {
  197.         lineShape.SetLineStart(rect.left, xy);
  198.         lineShape.SetLineEnd(rect.right - FW_kFixedPos1, xy);
  199.         lineShape.Render(gc);
  200.     }
  201.     
  202.     for(xy = rect.left, rc = 0;  xy < rect.right; xy += fTableContent->GetWidth(rc++) + kBorderHeight)
  203.     {
  204.         lineShape.SetLineStart(xy, rect.top);
  205.         lineShape.SetLineEnd(xy, rect.bottom - FW_kFixedPos1);
  206.         lineShape.Render(gc);
  207.     }
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. //    CTableView::DoMouseDown
  212. //----------------------------------------------------------------------------------------
  213.  
  214. FW_Handled CTableView::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  215. {
  216.     FW_Handled result = FW_kNotHandled;
  217.  
  218.     if (fGridShown)
  219.     {        
  220.         if (fTableFrame->GetSelection(ev)->IsMouseInDraggableItem(ev, fTableFrame, theMouseEvent, FALSE) &&
  221.             fTableFrame->Drag(ev, theMouseEvent))
  222.         {
  223.             result = FW_kHandled;
  224.         }
  225.         else 
  226.         {    
  227.             CCell cell;
  228.             ETableLoc tl = fTableContent->HitTest(ev, theMouseEvent, this, cell);
  229.             
  230.             if (tl != kTLNone && tl != kTLCell)
  231.                 this->Resize(ev, theMouseEvent, cell, tl);
  232.             
  233.             result = FW_kHandled;
  234.         }
  235.     }
  236.     
  237.     return result;
  238. }
  239.     
  240. //----------------------------------------------------------------------------------------
  241. //    CTableView::AdjustCursor
  242. //----------------------------------------------------------------------------------------
  243. // Where is in Frame coordinate
  244. FW_Handled CTableView::AdjustCursor(Environment* ev, ODFacet* odFacet, const FW_CPoint& where, ODEventInfo* eventInfo)
  245. {
  246.     FW_Handled cursorAdjusted = FW_kNotHandled;
  247.     
  248.     //     I don't want the Open Hand Cursor if I have the grid hidden because I am not supposed to
  249.     //    be able to move anything
  250.     if (fGridShown)
  251.         cursorAdjusted = FW_CSuperView::AdjustCursor(ev, odFacet, where, eventInfo);
  252.     
  253.     if (!cursorAdjusted && fGridShown)
  254.     {
  255.         CCell cell;
  256.         FW_CPoint temp(where);
  257.         FrameToViewContent(ev, temp);
  258.         ETableLoc tl = fTableContent->HitTest(ev, temp, cell);
  259.             
  260.         cursorAdjusted = FW_kHandled;
  261.         
  262.         if (tl == kTLCell || tl == kTLNone)
  263.             FW_gArrowCursor.Select();
  264.         else if (tl == kTLLeftBorder || tl == kTLRightBorder)
  265.             FW_gSizeWECursor.Select();
  266.         else if (tl == kTLTopBorder || tl == kTLBottomBorder)
  267.             FW_gSizeNSCursor.Select();
  268.         else 
  269.             FW_gSizeNWSECursor.Select();
  270.     }
  271.     
  272.     return cursorAdjusted;
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. //    CTableView::Resize
  277. //----------------------------------------------------------------------------------------
  278.  
  279. FW_Boolean CTableView::Resize(Environment* ev, 
  280.                                const FW_CMouseEvent& theMouseEvent,
  281.                                const CCell& cell,
  282.                                ETableLoc tl)
  283. {
  284.     FW_ASSERT(tl != kTLNone && tl != kTLCell);
  285.     FW_Boolean result = FALSE;
  286.  
  287.     // maximum rectangle
  288.     FW_CRect maxRect(FW_kZeroPoint, fTableContent->GetExtent());
  289.     maxRect.right  -= kPenWidth;
  290.     maxRect.bottom -= kPenHeight;
  291.  
  292.     // Location of borders
  293.     FW_CRect cellRect;
  294.     fTableContent->FindRect(cell, cellRect);
  295.     
  296.     FW_CPoint borders;
  297.     if ((tl & kTLLeftBorder) != 0)
  298.     {
  299.         maxRect.left = ((cell.fX == 0) ? cellRect.left : fTableContent->FindLeft(cell.fX - 1)) + kBorderWidth;
  300.         borders.x = cellRect.left;
  301.     }
  302.     else 
  303.     {
  304.         maxRect.left = cellRect.left + kBorderWidth;
  305.         borders.x = cellRect.right;
  306.     }
  307.         
  308.     if ((tl & kTLTopBorder) != 0)
  309.     {
  310.         maxRect.top = ((cell.fY == 0) ? cellRect.top : fTableContent->FindTop(cell.fY - 1)) + kBorderHeight;
  311.         borders.y = cellRect.top;
  312.     }
  313.     else 
  314.     {
  315.         maxRect.top = cellRect.top + kBorderHeight;
  316.         borders.y = cellRect.bottom;
  317.     }
  318.     
  319.  
  320.     // Create a tracker for the grid lines
  321.     CGridLineTracker tracker(ev, this, theMouseEvent.GetFacet(ev), tl, maxRect, borders);
  322.  
  323.     if (tracker.Track(ev, theMouseEvent))
  324.     {
  325.         // Set the new cell size
  326.         FW_CPoint delta = tracker.GetNewBorders() - tracker.GetOldBorders();
  327.         if (delta.x != FW_kFixed0 || delta.y != FW_kFixed0)
  328.         {
  329.             fTableContent->Resize(ev, cell, tl, delta);
  330.             fTableFrame->GetPresentation(ev)->Invalidate(ev);
  331.             result = TRUE;
  332.         }
  333.     }
  334.  
  335.     return result;
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. //    CTableView::Create
  340. //----------------------------------------------------------------------------------------
  341.  
  342. void* CTableView::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  343. {
  344. FW_UNUSED(stream);
  345. FW_UNUSED(type);
  346.  
  347.     FW_SOMEnvironment ev;
  348.     return new CTableView(ev);
  349. }
  350.  
  351. //----------------------------------------------------------------------------------------
  352. //    CTableView::Destroy
  353. //----------------------------------------------------------------------------------------
  354.  
  355. void CTableView::Destroy(void* object, FW_ClassTypeConstant type)
  356. {
  357. FW_UNUSED(type);
  358.  
  359.     CTableView* self = (CTableView*) object;
  360.     delete self;
  361. }
  362.  
  363. //----------------------------------------------------------------------------------------
  364. //    CTableView::DoActivateEvent
  365. //----------------------------------------------------------------------------------------
  366.  
  367. void CTableView::DoActivateEvent(Environment* ev, const FW_CActivateEvent& theActivateEvent)
  368. {
  369.     if (fTableFrame->HasSelectionFocus(ev))
  370.         ChangeHighlightState(ev, theActivateEvent.IsActivating(ev) ? kODFullHighlight : kODDimHighlight);
  371. }
  372.  
  373. //----------------------------------------------------------------------------------------
  374. //    CTableView::ChangeHighlightState
  375. //----------------------------------------------------------------------------------------
  376.  
  377. void CTableView::ChangeHighlightState(Environment* ev, ODHighlight highlight)
  378. {
  379.     if (!fGridShown)
  380.         highlight = kODNoHighlight;
  381.         
  382.     if (highlight == fHighlight)
  383.         return;
  384.     
  385.     CCell cell = fTableFrame->GetSelection(ev)->GetSelectedCell();
  386.     
  387.     if (((fHighlight == kODNoHighlight || fHighlight == kODDimHighlight) && (highlight == kODFullHighlight)) ||
  388.         (highlight == kODNoHighlight || highlight == kODDimHighlight) && (fHighlight == kODFullHighlight))
  389.     {
  390.         FW_CFrameFacetIterator ite(ev, fTableFrame);
  391.         for (ODFacet* facet = ite.First(ev); ite.IsNotComplete(ev); facet = ite.Next(ev))
  392.         {        
  393.             FW_CViewContext fc(ev, this, facet);
  394.             DrawCellHighlight(ev, fc, cell);
  395.         }
  396.     }
  397.     
  398.     fHighlight = highlight;
  399.  
  400.     CTableProxy* proxy = fTableContent->CellToProxy(cell);
  401.     if (proxy)
  402.         proxy->ChangeHighlight(ev, fHighlight, fTableFrame);
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. // CTableView::HideShowGrid
  407. //----------------------------------------------------------------------------------------
  408.  
  409. void CTableView::HideShowGrid(Environment* ev)
  410. {
  411.     fGridShown = !fGridShown;
  412.     fTableFrame->ChangeDroppableState(ev, fGridShown ? FW_kFrameDroppable : FW_kNotDroppable);
  413.     
  414.     FW_CFrameFacetIterator ite(ev, fTableFrame);
  415.     for (ODFacet* facet = ite.First(ev); ite.IsNotComplete(ev); facet = ite.Next(ev))
  416.     {        
  417.         FW_CViewContext fc(ev, this, facet);    
  418.         DrawGrid(ev, fc, fGridShown);
  419.     }    
  420.  
  421.     ChangeHighlightState(ev, fGridShown ? kODFullHighlight : kODNoHighlight);
  422. }
  423.  
  424. //----------------------------------------------------------------------------------------
  425. //    CTableView::DoMenu
  426. //----------------------------------------------------------------------------------------
  427.  
  428. FW_Handled CTableView::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  429. {
  430.     FW_Handled result = FW_kHandled;
  431.     ODCommandID commandID = theMenuEvent.GetCommandID(ev);
  432.  
  433.     switch(commandID)
  434.     {
  435.         case cHideShowGrid:
  436.             HideShowGrid(ev);
  437.             break;
  438.  
  439.         default:
  440.             result = FW_kNotHandled;
  441.             break;
  442.     }
  443.     
  444.     return result;
  445. }
  446.  
  447. //---------------------------------------------------------------------------------------
  448. //    CTableView::DoAdjustMenus
  449. //---------------------------------------------------------------------------------------
  450.  
  451. FW_Handled CTableView::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
  452. {
  453. FW_UNUSED(isRoot);
  454.     if (hasMenuFocus)
  455.     {
  456.         FW_Boolean hasSelection = !fTableFrame->GetSelection(ev)->IsEmpty(ev);
  457.         FW_Boolean canEmbedInCell = fGridShown && !hasSelection;
  458.         
  459.         menuBar->EnableAndToggleCommand(ev, cHideShowGrid, TRUE, fGridShown);
  460.         
  461.         // If an embedded part is selected, change Copy item to "Copy Part"
  462.         FW_CString32 itemName;
  463.         if (hasSelection)
  464.             itemName = "Copy Part";
  465.         else
  466.             itemName = "Copy";
  467.         menuBar->SetItemString(ev, kODCommandCopy, itemName);
  468.  
  469.         menuBar->EnableCommand(ev, kODCommandCopy, fGridShown && hasSelection);
  470.         menuBar->EnableCommand(ev, kODCommandClear, fGridShown && hasSelection);
  471.         menuBar->EnableCommand(ev, kODCommandCut, fGridShown && hasSelection);
  472.  
  473.         menuBar->EnableCommand(ev, kODCommandInsert, canEmbedInCell);
  474.  
  475.         // Check clipboard for pastable property
  476.         menuBar->EnableCommand(ev, kODCommandPaste, canEmbedInCell && fTableFrame->HasPropertyOnClipboard(ev, kODPropContentFrame, NULL));
  477.         menuBar->EnableCommand(ev, kODCommandPasteAs, canEmbedInCell && fTableFrame->HasPropertyOnClipboard(ev, kODPropContentFrame, NULL));
  478.  
  479.         // Because all my embedded frames are always selected I need to do my own enabling
  480.         menuBar->EnableCommand(ev, kODCommandOpen, hasSelection);
  481.     }
  482.     
  483.     return FW_kNotHandled;
  484. }
  485.  
  486. //----------------------------------------------------------------------------------------
  487. //    CTableView::PostCreateViewFromStream
  488. //----------------------------------------------------------------------------------------
  489. // Initialize back pointers
  490.  
  491. void CTableView::PostCreateViewFromStream(Environment* ev)
  492. {
  493.     fTableFrame = (CTableFrame*)GetFrame(ev);
  494.     FW_ASSERT(fTableFrame != NULL);
  495.     
  496.     fTablePart = (CTablePart*)fTableFrame->GetPart(ev);
  497.  
  498.     fTableContent = fTablePart->GetTableContent(ev);
  499.     FW_ASSERT(fTableContent != NULL);
  500.     
  501.     fTableContent->UpdateExtent(ev);
  502. }
  503.  
  504. //----------------------------------------------------------------------------------------
  505. //    CTableView::InternalTransformChanged
  506. //----------------------------------------------------------------------------------------
  507.  
  508. void CTableView::InternalTransformChanged(Environment *ev)
  509. {
  510.     // InternalTransformChanged can be called before fDrawPart as been initialized
  511.     if (fTablePart != NULL)
  512.     {
  513.         FW_CFacetClipper facetClipper;
  514.         facetClipper.Clip(ev, fTableFrame->GetPresentation(ev), NULL);
  515.     }
  516. }
  517.